<?php
/**
 * Description of StatusDAO
 *
 * @author Magno
 */
class StatusDAO {

    private $conexao;
    public function __construct() {
        $this->conexao = Conexao::getConexao();
    }

    public function salvar($status) {

        try {

            if($status->getStatusID() != null && $status->getStatusID() > 0) {
                $statment = $this->conexao->prepare("UPDATE status SET
                        descricao = :descricao,
                        tipo = :tipo
                        WHERE statusID = :statusID");

            }else {
                $statment = $this->conexao->prepare("INSERT INTO status(statusID,descricao,tipo) VALUES (:statusID,:descricao,:tipo);");
            }

            if($statment->execute($status->toBD())) {

                if($status->getStatusID() == null || $status->getStatusID() <= 0)
                    $status->setStatusID($this->conexao->lastInsertId());

                return $status;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro StatusDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro StatusDAO:".$err->getMessage());
        }

    }

    public function excluir($statusID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM status WHERE statusID = :statusID");
            $statment->bindParam(':statusID',$statusID);

            if(!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro StatusDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro StatusDAO:".$err->getMessage());
        }

    }

    public function listar($pagina = 0,$tipo = -1) {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($tipo);
            $statuss = array();
            $statment = $this->conexao->query( "SELECT * FROM status e $where $limite" );

            foreach ( $statment->fetchAll(PDO::FETCH_SERIALIZE) as $row )
                $statuss[] = new StatusBean($row['statusID'], $row['descricao'], $row['tipo']);

            return $statuss;

        }catch(PDOException $err) {
            throw new Exception("Erro StatusDAO:".$err->getMessage());
        }
    }

    public function buscarPorID($statusID) {
        try {

            $row = $this->conexao->query( "SELECT * FROM status WHERE statusID = $statusID " )->fetch(PDO::FETCH_SERIALIZE);

            if(isset($row['statusID']) && $row['statusID'] > 0) {
                $status = new StatusBean($row['statusID'], $row['descricao'], $row['tipo']);
                return $status;
            }else {
                return null;
            }

        }catch(PDOException $err) {
            throw new Exception("Erro StatusDAO:".$err->getMessage());
        }
    }

    private function getWhere($tipo = -1) {
        $where = " WHERE 1 = 1 ";

        if (isset ($tipo) && $tipo > 0) {
            $where = $where." AND e.tipo = $tipo ";
        }

        return $where;
    }

    public function total($tipo = -1) {
        try {

            $where = $this->getWhere($tipo);
            $total = $this->conexao->query( "SELECT COUNT(e.statusID) total FROM status e $where" )->fetch(PDO::FETCH_COLUMN);
            return $total;

        }catch(PDOException $err) {
            throw new Exception("Erro StatusDAO:".$err->getMessage());
        }
    }

}
?>
